
package com.sgcc.isc.config;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.sgcc.isc.service.adapter.factory.agent.FrontLoadedAgantManage;
import com.sgcc.isc.service.adapter.factory.agent.IFrontLoadedAgent;
import com.sgcc.isc.service.adapter.factory.agent.config.CacheConfig;
import com.sgcc.isc.service.adapter.factory.agent.config.LogConfig;
import com.sgcc.isc.service.adapter.factory.agent.config.ServiceAgentConfiguration;

@Component
public class IscPostConstruct {
	@Value("${isc.url}")
    private String url;

    @Value("${isc.jmsUrl}")
    private String jmsUrl;

    @Value("${isc.appid}")
    private String appid;

    @Value("${isc.isStartUp}")
    private Boolean isStartUp;

//	@PostConstruct
 //   public void init() {
//		if(isStartUp) {
//		if(false) {
//			runEx();
//		}

  //  }
	int i=0;
	private void runEx() {
		i++;
		try {
			System.out.println("重试第"+i+"次");
			run();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			runEx();

		}
	}


	private void run() {
		 // some init function
		IFrontLoadedAgent agent = FrontLoadedAgantManage.getInstance();
    	//配置参数
    	ServiceAgentConfiguration agentConfig = new ServiceAgentConfiguration(url);
    	agentConfig.setAppId(appid);
    	CacheConfig cacheConfig = new CacheConfig("failover:("+jmsUrl+")", appid);
    	cacheConfig.setIscache(true);
    	agentConfig.setCacheConfig(cacheConfig);
    	LogConfig logConfig = new LogConfig("failover:("+jmsUrl+")",appid);
    	logConfig.setIslog(false);
    	agentConfig.setLogConfig(logConfig);
    	//设置前置节点配置信息
    	agent.setConfigProps(agentConfig);
    	//启动缓存jms，缓存服务，日志服务，初始化服务容器
    	agent.startupAll();
    	System.out.println("ISC启动结束");
//    	关闭缓存服务，缓存jms，日志服务，一般在业务应用关闭时一起关闭
//    	agent.shutdownAll();
	}
}

